#!/bin/sh
#
# /etc/init.d/pure-ftpd
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# defaults
DAEMON="pure-ftpd"
NAME="pure-ftpd"
PREFIX="pure-ftpd: "
DAEMON_OPTIONS="-B @DAEMON_ARGS@"
DAEMON_PIDFILE=/var/run/pure-ftpd.pid

HELPER="pure-uploadscript"
HELPER_START_SCRIPT="@HELPER_SCRIPT@"
HELPER_OPTIONS="-B @HELPER_ARGS@"

trap "" 1
trap "" 15

which $DAEMON || { echo "${PREFIX}$DAEMON not found"; exit; }


if [ ! -z $HELPER_START_SCRIPT ] ; then
	DAEMON_OPTIONS="$DAEMON_OPTIONS -o"
fi

start_helper(){
	if [ ! -z $HELPER_START_SCRIPT ]; then
		which $HELPER || { echo "${PREFIX}$HELPER not found"; exit; }
		echo "${PREFIX}starting upload helper daemon..."
		$HELPER $HELPER_OPTIONS -r $HELPER_START_SCRIPT
	else
		echo "${PREFIX}no upload script defined, skipping"
	fi
}

stop_helper(){
	killall -9 "$HELPER"
}

start_daemon(){
	which $DAEMON
	$DAEMON -g $DAEMON_PIDFILE $DAEMON_OPTIONS
}

stop_daemon(){
	if [ -e "$DAEMON_PIDFILE" ]; then
		kill `cat $DAEMON_PIDFILE`
		case $? in
			0)
			echo "${PREFIX}$NAME stopped"
			;;
			*)
			kill -9 `cat $DAEMON_PIDFILE`
			echo "${PREFIX}failed to stop $NAME - process killed"
			;;
		esac
		rm -f $DAEMON_PIDFILE
		echo "${PREFIX}pidfile removed"
	else
		echo "${PREFIX}pid file not found - process not running"
	fi
}

start(){
	start_daemon
	start_helper
}

stop(){
	stop_daemon
	stop_helper
}

case "$1" in
    start)
	    echo -n "${PREFIX}starting $NAME: "
	    start
	    echo "done"
	;;

    stop)
	    echo -n "${PREFIX}stopping $NAME: "
	    stop
	    echo "done"
	;;

    reload)
	echo -n "${PREFIX}reloading $NAME configuration..."
	if [ -e "$DAEMON_PIDFILE" ]; then
	   kill -HUP `cat $DAEMON_PIDFILE`
	   echo " done."
	else
	   echo "pid file not found - process not running"
	fi
	;;

    force-reload|restart)
	    echo -n "${PREFIX}restarting $NAME."
	    stop
	    echo -n "."
	    sleep 2
	    echo -n "."
	    start
	    echo " done."
	;;

    *)
	echo "Usage: /etc/init.d/$NAME {start|stop|reload|restart|force-reload|help}"
	exit 1
	;;
esac

exit 0

